package com.atguigu.lease.common.exceptions;

import com.atguigu.lease.common.result.Result;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/*
    全局异常处理 | 声明式异常处理
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    /*
        public static <T> Result<T> build(int code,String message) {
            Result<T> result = new Result<>();
            result.setCode(code);
            result.setMessage(message);
            return result;
        }
     */
    @ExceptionHandler(LeaseException.class)
    public Result leaseExceptionHandler(LeaseException e){
        e.printStackTrace();
        int code = e.getCode();
        String message = e.getMessage();
        return Result.build(code,message);
    }

    @ExceptionHandler
    public Result exceptionHandler(Exception e){
        e.printStackTrace();
        Result<Object> fail = Result.fail();
        return fail;

    }
}
